package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.dto.DishDto;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.entity.DishFlavor;
import com.itheima.reggie.mapper.DishMapper;
import com.itheima.reggie.service.DishFlavorService;
import com.itheima.reggie.service.DishService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.List;

@Service
@Slf4j
public class DishServiceImpl extends ServiceImpl<DishMapper,Dish> implements DishService {
    @Autowired
    private DishFlavorService dishFlavorService;
    @Autowired
    private DishMapper dishMapper;

    /**
     * 新增菜品
     * @param dishDto
     */
    @Override
    public void saveWithFlavor(@RequestBody DishDto dishDto) {
        //保存菜品基本数据
        this.save(dishDto);
        Long disId = dishDto.getId();
        //获取dishDto的flavors属性，并且为止加上dishId属性值
        List<DishFlavor> flavors = dishDto.getFlavors();
        for (DishFlavor flavor : flavors) {
            flavor.setDishId(disId);
        }
        //保存数据到菜品口味表
        dishFlavorService.saveBatch(flavors);
    }

    /**
     * 根据ID获取菜品基本信息和菜品口味信息；修改时回显数据
     * @param id
     * @return
     */
    @Override
    public DishDto getByIdWithFlavor(Long id) {
        //根据ID获得菜品基本属性
        Dish dish = this.getById(id);
        //根据ID获得菜品口味属性
        LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DishFlavor::getDishId, id);
        List<DishFlavor> flavorList = dishFlavorService.list(queryWrapper);
        //封装到DishDto中
        DishDto dishDto = new DishDto();
        BeanUtils.copyProperties(dish,dishDto);
        dishDto.setFlavors(flavorList);

        return dishDto;
    }

    @Override
    public boolean updateWithFlavor(DishDto dishDto) {
        //更新菜品基本属性
        this.updateById(dishDto);
        //根据ID获得菜品口味属性
        LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DishFlavor::getDishId, dishDto.getId());
        //先删除后添加
        dishFlavorService.remove(queryWrapper);
        //因为flavors里的flavor属性没有dishID这个属性，所以需要赋值
        List<DishFlavor> flavors = dishDto.getFlavors();
        for (DishFlavor flavor : flavors) {
            //flavor的dishId就是dishDto的id属性值；给新增的口味来设置dishId的值
            flavor.setDishId(dishDto.getId());
        }
        return dishFlavorService.saveBatch(flavors);
    }

    @Override
    public boolean deleteByIds(List<Long> ids) {
        //查询菜品状态，停售的菜品才可以删除
        LambdaQueryWrapper<Dish> qw = new LambdaQueryWrapper<>();
        qw.in(Dish::getId, ids);
        qw.eq(Dish::getStatus, 0);
        //影响的行数
        int count = dishMapper.delete(qw);
        if (count != ids.size()) {
            //有数据未被删除成功
            throw new CustomException("菜品正在售卖中，不能删除");
        }
        //删除菜品数据成功,还要删除菜品-菜品口味关联表中数据
        LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper<>();
        //如果关联数据则删除
        queryWrapper.in(DishFlavor::getDishId, ids);
        dishFlavorService.remove(queryWrapper);
        return true;
    }

    /**
     * 分页查询
     * @param page
     * @param pageSize
     * @param name 菜品名
     * @return
     */
    @Override
    public Page<DishDto> pages(int page, int pageSize,String name) {
        //构造page,使用page和pageSize来构造
        Page<DishDto> dtoPage = new Page<>(page,pageSize);
        //如果name不为空,对name做处理
        if (name != null) {
            name = "%" + name + "%";
        }
        //查询数据并且封装到page中
        List<DishDto> dtoList = dishMapper.findBypage(dtoPage, name);
        dtoPage.setRecords(dtoList);
        return dtoPage;
    }

    /**
     * 前端查询菜品
     * @param categoryId 菜品id
     * @param status 菜品状态
     * @return
     */
    @Override
    public List<DishDto> listAll(Long categoryId,Integer status) {
        List<DishDto> dishDtos = dishMapper.listAll(categoryId,status);
        return dishDtos;
    }
}
